<?php
/**
 * @author ran.ran
 * pc登陆
 */
namespace liketry\controllers;

use Yii;
use yii\web\Controller;
use common\helper\VerifyHelper;
use linslin\yii2\curl\Curl;


class LoginController extends Controller
{
    
    public $enableCsrfValidation = false;
    public $layout = false;
    
    public function  actions(){
        return [
            'verify' => [
                'class' => 'components\CaptchaAction',//加载验证码
                'platform'=>false,//目前兼容老平台验证码session使用，改版登陆注册去掉。
            ],
        ];
    }
    
    /**
     * 动态加载弹框html
     */
    public function actionAjaxLogin(){
        
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        
        $html = $this->renderPartial('@liketry/views/layouts/login');
        
        return ['html'=>$html];
    }
    
    
    /**
     * 检查登陆
     */
    public function actionCheckLogin(){
        
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $params = Yii::$app->request->post();
        if(empty($params)) {
            return ['flag'=>1,'desc'=>'用户名不能为空'];
        }
        //如果验证码输入后
        if(!empty($params['login_yzm'])) {
            $verify = new VerifyHelper();
            if(!$verify->check($params['login_yzm'])) {
                return ['flag'=>1,'desc'=>'验证码错误，请重新输入'];
            } 
        }
        
        unset($params['login_yzm']);
        
        $uri = Yii::$app->params['domains']['liketry_url'].'auth/login_ajax.php';
        
        $curl = new Curl();
        $response = $curl->setOption(CURLOPT_POSTFIELDS, http_build_query($params))->post($uri);
        
        if(empty($response)) {
             return ['flag'=>1,'desc'=>'请求失败，稍后重试'];
        }
        
        return json_decode($response,true);
        
    }

}
